<?php
/**
 * Created by PhpStorm.
 * User: yuyan
 * Date: 2019/1/6
 * Time: 13:20
 */

namespace app\shop\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Hook;
use app\shop\model\Artwork;
use app\shop\model\Address;
use app\shop\model\AuctionOrder as AuctionOrderModel;
use app\shop\model\User;

class AuctionOrder extends Admin
{
    public function index()
    {
        $data_list = AuctionOrderModel::order('status asc,id desc')->paginate();
        $arr = json_decode(json_encode($data_list),true);
        $artworkIds = array_column($arr['data'],'artwork_id');
        //商品名称
        $artwork = Artwork::where('id','in',$artworkIds)->column('id,title');
        $status = [0=>'未扣款',1=>'下单',2=>'支付成功',3=>'订单完成',4=>'删除订单'];
        return ZBuilder::make('table')
            ->setPageTitle('常规订单')
            ->setSearch(['id' => 'ID'])
            ->addColumns([
                // ['id', 'ID'],
                ['order_num', '订单编号'],
                ['artwork_id', '拍卖品',$artwork],
                ['price', '成交价'],
                ['status', '状态',$status],
                ['create_time', '创建时间', 'datetime'],
                ['right_button', '操作', 'btn']
            ])
            //   ->addTopButtons('delete') // 批量添加顶部按钮
            ->addRightButtons('edit') // 批量添加右侧按钮
            ->addFilter('status', $status)
            ->addFilter('artwork_id', $artwork)
            ->setRowList($data_list) // 设置表格数据
            ->fetch(); // 渲染页面
    }

    public function edit($id = null)
    {
        if($this->request->isPost()){
            $data = $this->request->post();
            if (AuctionOrderModel::update($data)) {
                $artwork = AuctionOrderModel::get($data['id']);
                Hook::listen('artist_user_edit', $artwork);
                $this->success('编辑成功',url('index'));
            } else {
                $this->error('编辑失败');
            }
        }
        $info = AuctionOrderModel::where('id',$id)->find()->toArray();
        //收货地址
        $addr = Address::where('user_id',$info['user_id'])->find()->toArray();
        $info['add_name'] = $addr['name'];
        $info['add_tel'] = $addr['mobile'];
        $info['address'] =$addr['country'].$addr['prov'].$addr['city'].$addr['district'].$addr['address1'];
        //拍卖人
        $user = User::where('id',$info['user_id'])->field('id,mobile')->find()->toArray();
        $info['user_mobile'] = $user['mobile'];
        //商品
        $artwork = Artwork::where('id',$info['artwork_id'])->field('id,title')->find()->toArray();
        $info['title'] = $artwork['title'];
        //订单状态
        $status = [0=>'未扣款',1=>'下单',2=>'支付成功',3=>'订单完成',4=>'删除订单'];
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('编辑') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['hidden', 'id'],
                ['static', 'order_num', '订单号'],
                ['static', 'title', '拍卖品'],
                ['static', 'price', '成交价'],
                ['text', 'real_price', '已扣款'],
                ['static', 'user_mobile', '拍卖人电话'],
                ['static', 'add_name', '收货人'],
                ['static', 'add_tel', '收货人联系方式'],
                ['static', 'address', '收获地址'],
                ['textarea', 'remark', '备注'],
                ['radio', 'status', '状态', '',$status]
            ])
            ->setFormData($info) // 设置表单数据
            ->fetch();
    }
}